      -- A program to perform quicksort on an array of numbers
      -- 4000 = p (beggining index of array being sorted)
      -- 4004 = r (end index of portion of the array being sorted)
      -- 4008 = beginning of array of nums
      -- 
      -- R30 = SP
      -- R31 = Ret Addr Reg
      -- R2 = Address of beginning of array (4008)
      -- R3 = p 
      -- R4 = r
      -- R5 = q (result of partition operation)
      -- R6 = p - r for comparison
      -- R7 = A[r] (x) in partition
      -- R8 = i in partition
      -- R9 = j in partition
      -- R10 = temp in partition
      -- R11 = r-j in partition used for loop comparison
      -- R12 = Address of A[j]
      -- R13 = Address of A[i]
      -- R14 = value at A[j]
      -- R15 = x - A[j] for if comparison in partition
      -- R16 = Address of A[r]
      -- R29 = temp reg mostly for multiplying index by 4 to get address
      --
      --
      -- SETUP
      -- Stack will be at Org5000 - R30 is SP
0:  ADDI R30, R0, 5000
      -- Load p
4:  ADDI R2, R0, 4000
8:  LW R3, 0(R2)
      -- Load r
12:  ADDI R2, R2, 4
16:  LW R4, 0(R2)
      -- R2 is address of beginning of array of numbers
20:  ADDI R2, R2, 4
      -- Initialize R31 to 0
24:  ADDI R31, R0, 0
      --
      --
      -- Quicksort algorithm
LABEL Quicksort
      -- Subtract: r - p for comparison
28:  SUB R6, R3, R4
      -- Branch if greater than or equal to 0, aka p >= r
32:  BGEZ R6, TheEnd
36:  NOP
      -- Store value of R31
40:  SW R31, 0(R30)
44:  ADDI R30, R30, 4
      -- Run Partition
48:  JAL Partition
52:  NOP
      -- Save values of p, r, and q
56:  SW R3, 0(R30)
60:  SW R4, 4(R30)
64:  SW R5, 8(R30)
68:  ADDI R30, R30, 12
      -- Set new values for r = q-1
72:  ADDI R4, R5, -1
      -- Recursively call quicksort
76:  JAL Quicksort
80:  NOP
      -- restore values for q, r, and p
84:  ADDI R30, R30, -4
88:  LW R5, 0(R30)
92:  ADDI R30, R30, -4
96:  LW R4, 0(R30)
100:  ADDI R30, R30, -4
104:  LW R3, 0(R30)
      -- Set new values for p = q+1
108:  ADDI R3, R5, 1
      -- Recursively call quicksort
112:  JAL Quicksort
116:  NOP
      -- Restore R31
120:  ADDI R30, R30, -4
124:  LW R31, 0(R30)
128:  NOP
      -- If p >= r TheEnd is run. Simply jump back to R31
LABEL TheEnd
132:  BEQ R31, R0, Done
136:  NOP
140:  JR R31
144:  NOP
      --
      --
      -- Partition code
LABEL Partition
      -- get the address of A[r] - store r*4 in temp reg then add the start index of the array to it
148:  SLL R29, R4, 2
152:  ADD R16, R29, R2
      -- Load A[r] into R7 (x)
156:  LW R7, 0(R16)
      -- Set R8 (i) -> i = p-1
160:  ADDI R8, R3, -1
      -- Set j=p
164:  ADD R9, R0, R3
      -- Begin the loop in partition
LABEL PartitionLoop
      -- perform r-j for comparison
168:  SUB R11, R4, R9
      -- Branch if j >= r, aka r-j <= 0
172:  BLEZ R11, PostPartitionLoop
176:  NOP
      -- store j*4 in temp reg
180:  SLL R29, R9, 2
      -- Add the beginning of array to it to get the address of A[j]
184:  ADD R12, R29, R2
      -- Get the value of A[j]
188:  LW R14, 0(R12)
      -- Store x-A[j] for comparison
192:  SUB R15, R7, R14
      -- Branch back to the loop if A[j] > x aka x-A[j] < 0
196:  BLTZ R15, EndLoop
200:  NOP
      -- Increment i
204:  ADDI R8, R8, 1
      -- Store i*4 in R29
208:  SLL R29, R8, 2
      -- Store complete address of A[i]
212:  ADD R13, R29, R2
      -- set temp = A[i]
216:  LW R10, 0(R13)
      -- Store A[j] in A[i]
220:  SW R14, 0(R13)
      -- Store temp in A[j]
224:  SW R10, 0(R12)
      -- Ending the Loop
LABEL EndLoop
      -- increment j
228:  ADDI R9, R9, 1
      -- continue to the next loop iteration
232:  J PartitionLoop
236:  NOP
      -- PostPartitionLoop
LABEL PostPartitionLoop
      -- Store i*4 in R29
240:  SLL R29, R8, 2
      -- Store complete address of A[i]
244:  ADD R13, R29, R2
      -- Load value of A[r] into R7
248:  LW R7, 0(R16)
      -- Load temp = A[i+1]
252:  LW R10, 4(R13)
      -- Store A[r] into A[i+1]
256:  SW R7, 4(R13)
      -- Store temp in A[r]
260:  SW R10, 0(R16)
      -- Set q = i+1
264:  ADDI R5, R8, 1
      -- return
268:  NOP
272:  JR R31
276:  NOP
LABEL Done
280:  HALT
